package com.youlu.campus.web.community.controller;

import com.youlu.campus.base.interactive.vo.ActivitySignUpRequest;
import com.youlu.campus.entity.VO.req.PointPlaceOrderVO;
import com.youlu.campus.entity.VO.req.QaCommitCommand;
import com.youlu.campus.entity.VO.req.UserCaQuery;
import com.youlu.campus.entity.enums.TaskBizType;
import com.youlu.campus.service.activity.QQMiniActivityService;
import com.youlu.campus.service.activity.UserParticipateActivityInfoService;
import com.youlu.campus.service.ca.UserCaService;
import com.youlu.campus.service.qa.UserQaInfoService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
public class QQMiniActivityPlusController {

    @Autowired
    private UserCaService userCaService;

    @Autowired
    private QQMiniActivityService qqMiniActivityService;

    @Autowired
    private UserQaInfoService userQaInfoService;

    @Autowired
    private UserParticipateActivityInfoService userParticipateActivityInfoService;

    /**
     * 查看个人荣誉
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/user-ca/honor")
    public QResult userQaList(@RequestParam("activityId") String activityId) {
        return QResult.success(userCaService.findByActivityId(RequestContext.getCurUser(), activityId));
    }

    /**
     * 查看用户勋章
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/user-badge")
    public QResult userBadgeList(@RequestParam("activityId") String activityId) {
        return QResult.success(qqMiniActivityService.userBadgeList(RequestContext.getCurUser(), activityId));
    }

    /**
     * 首页活动列表
     *
     * @param
     * @return
     */
    @GetMapping("/public/activity/detail")
    public QResult index(@RequestParam("appId") String appId) {
        return QResult.success(qqMiniActivityService.detail(appId));
    }

    /**
     * 获取活动答题信息
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/qa/detail")
    public QResult findQaDetail(@RequestParam("activityId") String activityId) {
        return QResult.success(qqMiniActivityService.findQaDetailActivityId(activityId));
    }


    /**
     * 答题提交-每日挑战
     *
     * @param
     * @return
     */
    @PostMapping("/mini-app/task/user-qa/commit")
    public QResult qaCommit(@RequestBody @Validated QaCommitCommand commitCommand, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        commitCommand.setCurrentUser(RequestContext.getCurUser());
        commitCommand.setType(3);
        return QResult.success(userQaInfoService.doQaCommit(commitCommand));
    }

    /**
     * 授权用户进入活动主页
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/activity/home-page")
    public QResult homePageByActivityId(@RequestParam("activityId") String activityId) {
        return QResult.success(qqMiniActivityService.pageHome(RequestContext.getCurUser(),
                RequestContext.getAppId(),activityId));
    }

    /**
     * 匿名用户进入活动主页
     *
     * @param activityId
     * @return
     */
    @GetMapping("/public/activity/home-page")
    public QResult publicHomePageByActivityId(@RequestParam("activityId") String activityId,
                                              @RequestParam("appId") String appId) {
        return QResult.success(qqMiniActivityService.pageHome(RequestContext.getCurUser(),appId, activityId));
    }

    /**
     * 获取邀请进度
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/activity/invite-progress")
    public QResult activityInviteProgress(@RequestParam("activityId") String activityId) {
        return QResult.success(qqMiniActivityService.getInviteProgress(RequestContext.getCurUser(),1, activityId));
    }

    /**
     * 获取任务详情
     *
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/activity/task-detail")
    public QResult getTaskDetail(@RequestParam("activityId") String activityId, @RequestParam("taskBizType") TaskBizType taskBizType) {
        return QResult.success(qqMiniActivityService.getTaskDetail(activityId, taskBizType));
    }

    /**
     * 获取纸质版证书领取信息
     *
     * @return
     */
    @PostMapping("/mini-app/ca/recieve-info")
    public QResult getCaInfo(@NotNull @RequestBody UserCaQuery req) {
        req.setUserId(RequestContext.getUserId());
        req.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(qqMiniActivityService.getUserCaInfo(req));
    }

    /**
     * 纸质证书下单
     *
     * @param req
     * @return
     */
    @PostMapping("/mini-app/ca/place-order")
    public QResult getCaInfo(@NotNull @RequestBody PointPlaceOrderVO req) {
        req.setPlatform(RequestContext.getCurPlatform());
        req.setAppId(RequestContext.getAppId());
        req.setUserId(RequestContext.getUserId());
        return QResult.success(qqMiniActivityService.placeOrderByPoint(req, RequestContext.getCurUser()));
    }

    @PostMapping("/mini-app/plus/activity/sign-up")
    public QResult signUp(@RequestBody ActivitySignUpRequest request) {
        request.setAppId(RequestContext.getAppId());
        request.setPlatform(RequestContext.getCurPlatform());
        userParticipateActivityInfoService.signUpActivity(request, RequestContext.getCurUser());
        return QResult.success(null);
    }
}
